/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package compiladorcyclone;

/**
 *
 * @author Usuario
 */
public class funciones {

    static String funcion_resto_alu(String r1, String r2, String dir){
        String resul = r1 + r2 + dir;
        return resul;
    }

    static String funcion_reducir_inmediato(String num){
        if(num.contains("+")){
            num = num.replace("+", "");
        }
        Integer a= Integer.parseInt(num);

        return numeroABinarioNBits(a,8);
    }

    static String registroToString(String reg){
        String val = reg.substring(1);
        return numeroABinarioNBits(Integer.valueOf(val),3);
    }

    static String numeroABinarioNBits(int num, int bits){
        String res = Integer.toBinaryString(num);
        if(num < 0){
            res = res.substring(res.length()-bits);
        }
        else{
            for(int i = res.length(); i<bits; i++){
                res = "0" + res;
            }
        }
        
        return res;
    }

    static String hexadABinarioNBits(String hex, int bits){
        String num = hex.replace("0x", "");
        int dec = Integer.parseInt(num, 16);
        return funciones.numeroABinarioNBits(dec, bits);
    }

    }
